<?php
/**
 * Created by PhpStorm.
 * Author: Sojo
 * Date: 2017/2/25
 * Time: 17:21
 */
namespace App\Http\Admin\User\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Http\Admin\User\Models\Permission;
use App\Http\Admin\Website\Models\Menu;
use App\Http\Admin\User\Models\User;

class IndexController extends Controller
{
    /**
     * 获取用户列表
     * @author Sojo
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
     */
    public function getUserList()
    {
        if (request()->isMethod('post')) {
            $user = new User();
            $userList = $user->getUserList();

            return $userList->toJson();
        }

        return $this->view('list');
    }

    public function saveUser()
    {
        $params = $this->jsonRequest();

        try {
            $user = new User();
            $result = (isset($params->addFlag) && $params->addFlag)
                ? $user->addUser($params->username, $params->email, $params->status)
                : $user->updateUser($params->id, $params->email, $params->status);
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }

        return $this->jsonReturn($result);
    }

    public function deleteUser()
    {
        $userId = request('id');

        try {
            $user = new User();
            $result = $user->delRole($userId);
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }

        return $this->jsonReturn($result > 0);
    }

    public function roleAssignment()
    {
        if (request()->isMethod('post')) {
            $userId = request('user_id');
            $permission = new Permission();
            $roleList = $permission->getRoleListByRoleAssignment($userId);

            return $roleList->toJson();
        }
        $userId = request('userId');

        return $this->view('role-assignment', compact('userId'));
    }

    public function saveRoleAssignment()
    {
        $params = $this->jsonRequest();

        try {
            $permission = new Permission();
            $result = $permission->saveRoleAssignment($params->user_id, $params->id);

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }

        return $this->jsonReturn($result);
    }

    /**
     * 修改密码
     * @author Sojo
     */
    public function changePassword()
    {
        if (request()->isMethod('post')) {
            try {
                $params = request()->only('user_id', 'old_password', 'password', 'password_confirmation');
                $rules = [
                    'user_id' => 'required|integer',
                    'old_password' => 'required|min:6|max:32',
                    'password'     => 'required|min:6|max:32|confirmed',
                ];
                $this->validateRequestParameters($params, $rules);

                $user = new User();
                $result = $user->changePassword($params['user_id'], $params['old_password'], $params['password']);
            } catch (\DebugError $e) {
                return $this->ajaxFail($e->getMessage());
            }

            return $this->jsonReturn($result);
        }

        $user = request()->user('admin');
        $userId = $user->id;
        $username = $user->username;
        $roleName = $user->roles[0]->display_name;

        return $this->view('change-password', compact('userId', 'username', 'roleName'));
    }

    public function authorityManagement()
    {
        if (request()->isMethod('post')) {
            $permission = new Permission();
            $roleList = $permission->getRoleList();

            return $roleList->toJson();
        }

        return $this->view('authority-management');
    }

    public function saveRole()
    {
        $params = $this->jsonRequest();

        try {
            $permission = new Permission();
            $result = (isset($params->addFlag) && $params->addFlag)
                ? $permission->addRole($params->name, $params->display_name, $params->description)
                : $permission->updateRole($params->id, $params->name, $params->display_name, $params->description);
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }

        return $this->jsonReturn($result);
    }

    public function delRole()
    {
        $roleId = request('id');

        try {
            $permission = new Permission();
            $result = $permission->delRole($roleId);
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }

        return $this->jsonReturn($result > 0);
    }

    public function permissionAssignment()
    {
        if (request()->isMethod('post')) {
            $roleId = request('role_id');
            $menu = new Menu();
            $menuList = $menu->getMenuListByPermissionAssignment($roleId);

            return json_encode($menuList);
        }
        $roleId = request('roleId');

        return $this->view('permission-assignment', compact('roleId'));
    }

    public function savePermissionAssignment()
    {
        $params = $this->jsonRequest();

        try {
            $permission = new Permission();
            if (is_array($params)) {
                $result = $permission->multiSavePermissionAssignment($params[0]->role_id, $params);
            } else {
                $result = $permission->singleSavePermissionAssignment($params->role_id, $params->id, $params->permission);
            }

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }

        return $this->jsonReturn($result);
    }
}